//
//  CameraViewController.m
//  FlowChart
//
//  Created by yilong.zhang on 11-4-2.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CameraViewController.h"


@implementation CameraViewController

@synthesize m_CancelBtn;
@synthesize m_TakePhotoBtn;
@synthesize m_delegate;
@synthesize m_Action;
@synthesize m_Target;
@synthesize m_Image;
@synthesize m_PreView;

//methods
-(id)init
{
    self = [super init];
    if (self)
    {
        m_Action = nil;
        m_Target = nil;
        
        self.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        /*m_PreView = [[CUploadPreView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        m_PreView.alpha = 0.0;
        m_PreView.delegate = self;
        [self.view addSubview:m_PreView];
        [m_PreView release];*/
    }
    
    return  self;
}

-(id)initWithSize:(CGSize)size
{
    self = [super init];
    if (self)
    {
        m_Action = nil;
        m_Target = nil;
        
        self.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.showsCameraControls = NO;
        
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-68, self.view.frame.size.width, 68)];
        back.image = [UIImage imageNamed:@"tabbargb.png"];
        [self.view addSubview:back];
        [back release];
        
        m_CancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_CancelBtn.frame = CGRectMake(10, self.view.frame.size.height-50, 71, 48);
        [m_CancelBtn setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
        [m_CancelBtn addTarget:self action:@selector(OnCancelBtnDown:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:m_CancelBtn];
        
        m_TakePhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_TakePhotoBtn.frame = CGRectMake(0, self.view.frame.size.height-50, 92, 49);
        m_TakePhotoBtn.center = CGPointMake(self.view.frame.size.width/2, m_TakePhotoBtn.center.y);
        [m_TakePhotoBtn setBackgroundImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];
        m_TakePhotoBtn.tag = 1;
        [m_TakePhotoBtn addTarget:self action:@selector(TakeImages:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:m_TakePhotoBtn];
        
        m_PreView = [[CUploadPreView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) Size:size Cancel:NO];
        m_PreView.delegate = self;
        [self.view addSubview:m_PreView];
        [m_PreView release];
    }
    
    return  self;
}

-(void)OnCancelBtnDown:(UIButton*)sender
{
    self.m_Image = nil;
    [self dismissModalViewControllerAnimated:YES];
}

-(void)TakeImages:(UIButton*)sender
{
    if (0 == sender.tag)
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    else if (1 == sender.tag)
    {
        [self takePicture];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        m_PreView.m_ImgView.image = nil;
        [m_PreView.m_Indicator startAnimating];
        m_PreView.m_Indicator.alpha = 1.0;
        m_PreView.center = CGPointMake(self.view.frame.size.width/2, m_PreView.center.y);
        [UIView commitAnimations];
    }
    else {;}
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	self.m_Image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    [self ShowPreView];
}

-(void)ShowPreView
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
    [m_PreView SetImage:self.m_Image];
    return;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    //m_PreView.alpha = 1.0;
    m_PreView.center = CGPointMake(self.view.frame.size.width/2, m_PreView.center.y);
    [UIView commitAnimations];
}

-(void)DidUploaPreViewAtived:(UIView *)view Commond:(int)command Value1:(id)value1
{
    if (2 == command)
    {
        [self DidImageSelected:(UIImage* )value1];
    }
    else if (1 == command)
    {
        if (value1)
        {
            /*NSData *data = UIImageJPEGRepresentation(value1, 0.7);
            NSString *path = [[NSString stringWithFormat:@"%d.jpg", [[AppConfig DefaultAppConfig] DateToInt:[[AppConfig DefaultAppConfig] GetTimeFormatString]]] retain];
            [data writeToFile:[NSString stringWithFormat:@"%@/Documents/temp/%@", NSHomeDirectory(), path] atomically:YES];
            [[CUploadPageCtrl DefaultUploadPage].m_ImgPathes addObject:path];
            
            if (5 == [[CUploadPageCtrl DefaultUploadPage].m_ImgPathes count])
            {
                m_PreView.m_TakeAgain.enabled = NO;
                m_PreView.m_TakeAgain.alpha   = 0.5;
            }*/
            [m_delegate CImagePickerDidSelectImage:value1];
        }
    }
    else 
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        //m_PreView.alpha = 1.0;
        m_PreView.center = CGPointMake(self.view.frame.size.width/2+self.view.frame.size.width, m_PreView.center.y);
        [UIView commitAnimations];
    }
}

-(void)DidImageSelected:(UIImage*)image
{
    if (image)
    {
        /*NSData *data = UIImageJPEGRepresentation(image, 0.7);
        NSString *path = [[NSString stringWithFormat:@"%d.jpg", [[AppConfig DefaultAppConfig] DateToInt:[[AppConfig DefaultAppConfig] GetTimeFormatString]]] retain];
        [data writeToFile:[NSString stringWithFormat:@"%@/Documents/temp/%@", NSHomeDirectory(), path] atomically:YES];
        [[CUploadPageCtrl DefaultUploadPage].m_ImgPathes addObject:path];*/
        [m_delegate CImagePickerDidSelectImage:image];
    }
    self.m_Image = image;
    [self dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    self.m_Image = nil;
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
	return (interfaceOrientation == UIDeviceOrientationLandscapeRight);
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.delegate = self;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [m_Target performSelector:m_Action withObject:m_Image];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	
	////NSLog(@"CameraViewController_dealloc");
    [m_Image release];
    [super dealloc];
	
}


@end
